<template>
  <div>
    <h1>用户管理</h1>
    <el-table :data="users">
      <el-table-column label="用户名" prop="username">
        <template slot-scope="scope">
          <cl-editable v-model="scope.row.username"></cl-editable>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" prop="email">
        <template slot-scope="scope">
          <cl-editable v-model="scope.row.email"></cl-editable>
        </template>
      </el-table-column>
      <el-table-column label="手机" prop="tel">
        <template slot-scope="scope">
          <cl-editable v-model="scope.row.tel" @change="handleTelChange"
                       @toggle="handleToggle('tel', scope.row)"></cl-editable>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.available" size="mini" type="danger"
                     @click="handleDisable(scope.$index, scope.row)">禁用
          </el-button>
          <el-button v-else size="mini" type="info" @click="handleActive(scope.$index, scope.row)">启用</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
import ClEditable from '@/components/Admin/components/ClEditable'
import { AdminGetUsers, AdminPostUpdateUser, AdminDeleteUser } from '@/utils/request'

export default {
  components: {
    'cl-editable': ClEditable
  },
  data () {
    return {
      cache: '',
      users: [
        { email: 'asdfghj@456.com', tel: 12345678909, username: 'qwert', available: 'active' }
      ]
    }
  },
  methods: {
    handleTelChange (val, oldval) {
      this.cache = val
    },
    handleToggle (field, row) {
      AdminPostUpdateUser(row.id, {
        type: 'TEL',
        tel: this.cache
      }).then(rsp => {
        const { success } = rsp.data
        if (success) {
          row.available = false
          this.$notify({
            title: '已更新'
          })
        }
      })
    },
    handleDisable (index, row) {
      AdminPostUpdateUser(row.id, {
        type: 'AVAILABLE',
        flag: false
      }).then(rsp => {
        const { success } = rsp.data
        if (success) {
          row.available = false
          this.$notify({
            title: '已禁用'
          })
        }
      })
    },
    handleActive (index, row) {
      AdminPostUpdateUser(row.id, {
        type: 'AVAILABLE',
        flag: true
      }).then(rsp => {
        const { success } = rsp.data
        row.available = true
        if (success) {
          this.$notify({
            title: '已启用'
          })
        }
      })
    },
    handleDelete (index, row) {
      AdminDeleteUser(row.id).then(rsp => {
        const { success } = rsp.data
        if (success) {
          window.location.reload()
        }
      })
    }
  },
  created () {
    AdminGetUsers().then(rsp => {
      this.users = rsp.data
    })
  }
}
</script>

<style lang="scss" scoped>
</style>
